package org.zkoss.service;

import java.util.List;

import org.springframework.dao.DataAccessException;
import org.springframework.transaction.annotation.Transactional;
import org.zkoss.model.bean.Company;
import org.zkoss.model.bean.Contact;
import org.zkoss.model.dao.ContactDAO;

public class ContactManagerImpl implements ContactManager {

	private ContactDAO contactDAO;

	public ContactDAO getContactDAO() {
		return contactDAO;
	}

	public void setContactDAO(ContactDAO contactDAO) {
		this.contactDAO = contactDAO;
	}
	
	@Transactional
	public boolean save(Contact contact) {
		try {
			contactDAO.saveOrUpdate(contact);
			return true;
		} catch (DataAccessException e) {
			e.printStackTrace();
			return false;
		}
	}
	
	@Transactional
	public boolean delete(Contact contact) {
		try {
			contactDAO.delete(contact);
			return true;
		} catch (DataAccessException e) {
			e.printStackTrace();
			return false;
		}
	}
	
	@Transactional(readOnly = true)
	public List<Contact> findAllContact() {
		try {
			return contactDAO.findAll(Contact.class);
		} catch (DataAccessException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	@Transactional(readOnly = true)
	public List<Contact> findByCompany(Company company) {
		try {
			return contactDAO.findByCompany(company, Contact.class);
		} catch (DataAccessException e) {
			e.printStackTrace();
			return null;
		}
	}
}
